package com.qf.controller;

import com.qf.api.UserinfoApi;
import com.qf.po.Userinfo;
import com.qf.vo.JsonBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@Slf4j
public class UserinfoApiController {
    @Autowired(required = false)
    UserinfoApi userinfoApi;
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public JsonBean login(String username,String password){
        log.info(username+"====="+password);
        Userinfo userinfo = userinfoApi.userInfo(username,password);
        if (ObjectUtils.isEmpty(userinfo)){
            return new JsonBean(-200,"用户名或密码错误",null);
        }
        String password1 = userinfo.getPassword();
        if (password.equals(password1)){
            return new JsonBean(200,"登录成功",userinfo);
        }
        return new JsonBean(-200,"登录失败",null);
    }
    @RequestMapping(value = "/regost",method = RequestMethod.POST)
    public JsonBean regist(Userinfo userinfo){
        try {
            Userinfo useradd = userinfoApi.useradd(userinfo);
            return new JsonBean(200,"注册成功",userinfo);
        }catch (Exception e){
            return new JsonBean(-200,"注册失败",null);
        }
    }
    @RequestMapping(value = "/upd",method = RequestMethod.PUT)
    public JsonBean update(Userinfo userinfo){
        try {
            Userinfo update = userinfoApi.update(userinfo);
            return new JsonBean(200,"注册成功",update);
        }catch (Exception e){
            return new JsonBean(-200,"注册失败",null);
        }
    }
    @RequestMapping(value = "/del",method = RequestMethod.DELETE)
    public JsonBean del(Integer id){
            boolean delete = userinfoApi.delete(id);
            if (delete) {
                return new JsonBean(200, "删除成功", delete);
            }
            return new JsonBean(-200, "删除失败", delete);
    }
    @RequestMapping(value = "/sys",method = RequestMethod.GET)
    public JsonBean sys(String tel){
        String sms = userinfoApi.sms(tel);
        if (sms.equals("0")) {
            return new JsonBean(-200, "发送失败", null);
        }
        return new JsonBean(200, "发送成功", sms);
    }
}
